﻿//-----------------------------------------------------------------------
// <copyright file="GoatTokenizer.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace GoatGen.Token
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using Newtonsoft.Json;
    
    /// <summary>
    /// Class which handles tokenization of a Goat definition into tokens that
    /// we can use to build C# code from.
    /// </summary>
    public class GoatTokenizer
    {
        #region Fields - Private

        /// <summary>
        /// Name of the file that we are going to process.
        /// </summary>
        private string fileName;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for the tokenizer.
        /// </summary>
        /// <param name="fileName">File name we will process.</param>
        public GoatTokenizer(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName", "File name must be non-null.");
            }

            this.fileName = fileName;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Tokenize the input file and return a list of the tokens in it.
        /// </summary>
        /// <returns>Returns a list of tokens in the file. Will never be null.</returns>
        public IList<GoatToken> Tokenize()
        {
            try
            {
                IList<GoatToken> tokens = new List<GoatToken>();

                using (StreamReader streamReader = new StreamReader(this.fileName))
                {
                    JsonTextReader reader = new JsonTextReader(streamReader);

                    while (reader.Read())
                    {
                        GoatToken token = this.JsonTokenToGoatToken(reader);

                        tokens.Add(token);
                    }
                }

                return tokens;
            }
            catch (JsonException jsonException)
            {
                throw new GoatException("Failed to build parse Goat file: " + jsonException.ToString(), jsonException);
            }
        }

        #endregion

        #region Fields - Private

        /// <summary>
        /// Given the current state of the JSON reader, get its current token and convert
        /// it into a Goat token.
        /// </summary>
        /// <param name="reader">JSON reader we are using.</param>
        /// <returns>Returns the current Goat token.</returns>
        private GoatToken JsonTokenToGoatToken(JsonTextReader reader)
        {
            switch (reader.TokenType)
            {
                case JsonToken.StartObject:
                    return new GoatToken() { Type = GoatTokenType.StartObject };
                case JsonToken.EndObject:
                    return new GoatToken() { Type = GoatTokenType.EndObject };
                case JsonToken.PropertyName:
                    return new GoatToken() { Type = GoatTokenType.PropertyName, Value = reader.Value.ToString() };
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.String:
                case JsonToken.Boolean:
                case JsonToken.Date:
                    return new GoatToken() { Type = GoatTokenType.Value, Value = reader.Value.ToString() };
                default:
                    throw new GoatException("Encountered unknown token: " + reader.TokenType.ToString());
            }
        }

        #endregion
    }
}
